/*
 * Copyright (C) 2023 HiHope Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "hi_io.h"

#define BLINKER_PIN     9
#define BUTTON_PIN      5
#define TIMER_DELAY     (30)
#define TASK_STACK      512
#define TASK_PRIORITY   (25)
#define VOLTAGE_HIGH    (1)
#define VOLTAGE_LOW     (0)

typedef enum {
    LIGHT_ON = 0,
    LIGHT_OFF,
    LIGHT_FLICKER,
} LightStatus;

LightStatus currentLightMode = LIGHT_FLICKER;
static int loopCounter = 100;

static void *LightController(void)
{
    while (loopCounter) {
        switch (currentLightMode) {
            case LIGHT_ON:
                IoTGpioSetOutputVal(BLINKER_PIN, VOLTAGE_LOW);
                osDelay(TIMER_DELAY);
                break;
            case LIGHT_OFF:
                IoTGpioSetOutputVal(BLINKER_PIN, VOLTAGE_HIGH);
                osDelay(TIMER_DELAY);
                break;
            case LIGHT_FLICKER:
                IoTGpioSetOutputVal(BLINKER_PIN, VOLTAGE_LOW);
                osDelay(TIMER_DELAY);
                IoTGpioSetOutputVal(BLINKER_PIN, VOLTAGE_HIGH);
                osDelay(TIMER_DELAY);
                break;
            default:
                osDelay(TIMER_DELAY);
                break;
        }
    }

    return NULL;
}

static void HandleButtonPress(void)
{
    LightStatus nextMode = LIGHT_FLICKER;
    switch (currentLightMode) {
        case LIGHT_ON:
            nextMode = LIGHT_OFF;
            break;
        case LIGHT_OFF:
            nextMode = LIGHT_ON;
            break;
        case LIGHT_FLICKER:
            nextMode = LIGHT_OFF;
            break;
        default:
            break;
    }

    currentLightMode = nextMode;
    loopCounter--;
}

static void InitLightSystem(void)
{
    osThreadAttr_t taskConfig;

    IoTGpioInit(BLINKER_PIN);
    IoTGpioSetDir(BLINKER_PIN, IOT_GPIO_DIR_OUT);
 
    IoTGpioInit(BUTTON_PIN);
    hi_io_set_func(BUTTON_PIN, VOLTAGE_LOW);
    IoTGpioSetDir(BUTTON_PIN, IOT_GPIO_DIR_IN);
    hi_io_set_pull(BUTTON_PIN, VOLTAGE_HIGH);

    IoTGpioRegisterIsrFunc(BUTTON_PIN, IOT_INT_TYPE_EDGE,
        IOT_GPIO_EDGE_FALL_LEVEL_LOW,
        HandleButtonPress, NULL);

    taskConfig.name = "LightControlTask";
    taskConfig.attr_bits = 0U;
    taskConfig.cb_mem = NULL;
    taskConfig.cb_size = 0U;
    taskConfig.stack_mem = NULL;
    taskConfig.stack_size = TASK_STACK;
    taskConfig.priority = TASK_PRIORITY;

    if (osThreadNew((osThreadFunc_t)LightController, NULL, &taskConfig) == NULL) {
        printf("[LightSystem] Failed to initialize control task!\n");
    }
}
SYS_RUN(InitLightSystem);

